package book.java.effective;

/**
 * 不直接生成想要的对象，而是让客户端利用所有必要的参数调用构造器（或者静态工厂），得到一个builder对象，再调用类似setter的方法设置相关可选参数。
 * 
 * @author liubin
 *
 * @date 2018年3月2日上午9:36:59
 */
public class Student {
    /* 必填 */
    private String name;
    private int age;
    /* 选填 */
    private String sex;
    private String grade;

    public static class Builder {
        private String name;
        private int age;
        private String sex = "";
        private String grade = "";

        public Builder(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public Builder sex(String sex) {
            this.sex = sex;
            return this;
        }

        public Builder grade(String grade) {
            this.grade = grade;
            return this;
        }

        //
        public Student build() {
            return new Student(this);
        }
    }

    private Student(Builder builder) {
        this.name = builder.name;
        this.age = builder.age;
        this.sex = builder.sex;
        this.grade = builder.grade;
    }

    public static void main(String[] args) {
        Student student = new Student.Builder("kevin", 23).grade("1年级").build();
    }
}
